# -*- coding: utf-8 -*-  
# @File : 二维三次样条差值.py          
# @Time : 2024/11/19 1:49
# @Author : Alec
# @Func : 
# @Desc :

import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import CubicSpline

# 定义数据点
x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
y1 = np.array([0, 4, 6, 8, 9, 11, 13, 16, 19, 21, 23, 25, 28, 30, 33, 34])
y2 = np.array([0, 1, 2, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 25, 29, 34])
y3 = np.array([0, 8, 12, 18, 20, 26, 30, 32, 33, 34, 34, 36, 36, 35, 35, 34])

# 创建三次样条插值函数
cs1 = CubicSpline(x, y1)
cs2 = CubicSpline(x, y2)
cs3 = CubicSpline(x, y3)

# 生成插值后的x坐标
x_new = np.linspace(0, 15, 100)

# 计算插值后的y坐标
y_new1 = cs1(x_new)
y_new2 = cs2(x_new)
y_new3 = cs3(x_new)

# 设置颜色选择器和颜色映射
select2 = (0.1, 0.4, 0.5, 0.3, 0.8, 1.0)  # 非连续型色组在0-(色组长度-1)之间选择
colors = plt.get_cmap('rainbow')(select2)  # 从色组里选择颜色

# 绘制原始数据点和插值曲线
# 调整图表大小和位置
plt.figure(figsize = (10, 5))
plt.plot(x, y1, 'o', color = colors[0], label = 'ori_y1')
plt.plot(x, y2, 'o', color = colors[2], label = 'ori_y2')
plt.plot(x, y3, 'o', color = colors[4], label = 'ori_y3')
plt.plot(x_new, y_new1, '-', color = colors[3], label = 'y1_interp_curve')
plt.plot(x_new, y_new2, '-', color = colors[1], label = 'y2_interp_curve')
plt.plot(x_new, y_new3, '-', color = colors[5], label = 'y3_interp_curve')

# 设置图例和标题
plt.legend()
plt.xticks(np.arange(0, 16, 1))
plt.title('Cubic Spline Interpolation Example')
plt.xlabel('x')
plt.ylabel('y')

# 显示图表
plt.show()